package com.montran.sort;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@SuppressWarnings({"rawtypes","unchecked"})
public class ManagerSort {
	
	public void sortNewsByDateDecending(List<News> listNews) {
		Collections.sort(listNews, this.decending(this
				.getComparator(NewsComparator.DATE_SORT)));
	}
	
	public void sortNewsByDateAcending(List<News> listNews) {
		Collections.sort(listNews, this
				.getComparator(NewsComparator.DATE_SORT));
	}

	public void sortNewsByDateAndTitle(List<News> listNews) {
		Collections.sort(listNews,
				this.getComparator(NewsComparator.DATE_SORT, NewsComparator.TITLE_SORT));

	}

	private Comparator<Object> decending(final Comparator other) {
		return new Comparator<Object>() {
			public int compare(Object o1, Object o2) {
				return -1 * other.compare(o1, o2);
			}
		};
	}
	
	
	
	private Comparator getComparator(
			final Comparator... multipleOptions) {
		return new Comparator<News>() {
			public int compare(News o1, News o2) {
				for (Comparator option : multipleOptions) {
					int result = option.compare(o1, o2);
					if (result != 0) {
						return result;
					}
				}
				return 0;
			}
		};
	}
}
